package org.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.example.entity.Dormitory;
import org.example.entity.Moveout;
import org.example.entity.Student;
import org.example.form.SearchForm;
import org.example.mapper.DormitoryMapper;
import org.example.mapper.MoveoutMapper;
import org.example.mapper.StudentMapper;
import org.example.service.MoveoutService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.example.util.CommonUtil;
import org.example.vo.MoveoutVO;
import org.example.vo.PageVO;
import org.example.vo.StudentVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author admin
 * @since 2024-10-31
 */
@Service
public class MoveoutServiceImpl extends ServiceImpl<MoveoutMapper, Moveout> implements MoveoutService {
@Autowired
private DormitoryMapper dormitoryMapper;
@Autowired
private StudentMapper studentMapper;
@Autowired
private MoveoutMapper moveoutMapper;
    @Override
    public PageVO list(Integer page, Integer size) {
        QueryWrapper<Student> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("state","入住");
        Page<Student> studentPage = studentMapper.selectPage(new Page<>(page, size), queryWrapper);

        List<Student> studentlist = studentPage.getRecords();
        List<StudentVO> students =  new ArrayList<StudentVO>();
        for (Student student : studentlist) {
            StudentVO studentVO = new StudentVO();
            BeanUtils.copyProperties(student, studentVO);//将学生信息复制到学生VO中
            Dormitory dormitory = this.dormitoryMapper.selectById(student.getDormitoryId());
            studentVO.setDormitoryName(dormitory.getName());
            students.add(studentVO);
        }
        PageVO pageVO = new PageVO();
        pageVO.setData(students);
        pageVO.setTotal(studentPage.getTotal());
        return  pageVO;
    }

    @Override
    public PageVO search(SearchForm searchForm) {
        Page<Student> studentPage = new Page<>(searchForm.getPage(), searchForm.getSize());
        Page<Student> studentPage1 = null;
        QueryWrapper<Student> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("state","入住");
        List<StudentVO> students =  new ArrayList<StudentVO>();
        if(searchForm.getValue().equals("")){
            studentPage1 = studentMapper.selectPage(studentPage, queryWrapper);
            studentPage1.getRecords();

        }else{

          queryWrapper.like(searchForm.getKey(), searchForm.getValue());
            studentPage1 = this.studentMapper.selectPage(studentPage, queryWrapper);
            studentPage1.getRecords();
        }
        for (Student student : studentPage1.getRecords()) {
            StudentVO studentVO = new StudentVO();
            BeanUtils.copyProperties(student, studentVO);//将学生信息复制到学生VO中
            Dormitory dormitory = this.dormitoryMapper.selectById(student.getDormitoryId());
            studentVO.setDormitoryName(dormitory.getName());
            students.add(studentVO);
        }
        PageVO pageVO = new PageVO();
        pageVO.setData(students);
        pageVO.setTotal(studentPage1.getTotal());
        return  pageVO;
    }

    @Override
    public boolean moveout(Integer id, String reason) {
        Moveout moveout = new Moveout();
        moveout.setStudentId(String.valueOf(id));
        moveout.setReason(reason);
        Student student = this.studentMapper.selectById(id);
        moveout.setDormitoryId(String.valueOf(student.getDormitoryId()));
        moveout.setCreateDate(CommonUtil.createDate());
        int insert = this.moveoutMapper.insert(moveout);
        if(insert!= 1) return false;
        student.setState("迁出");
        int i = this.studentMapper.updateById(student);

        if(i != 1) return false;
        try {

            this.dormitoryMapper.addAvailable(student.getDormitoryId());
        } catch (Exception e) {

            return false;
        }
     return true;
    }

    @Override
    public PageVO moveoutList(Integer page, Integer size) {
        Page<Moveout> moveoutPage = moveoutMapper.selectPage(new Page<>(page, size), null);

        List<Moveout> moveoutList= moveoutPage.getRecords();
        List<MoveoutVO> moveouts =  new ArrayList<MoveoutVO>();
        for (Moveout moveout : moveoutList) {
            MoveoutVO moveoutVO = new MoveoutVO();
            BeanUtils.copyProperties(moveout, moveoutVO);//将迁出信息复制到迁出VO中
            Student student = this.studentMapper.selectById(Integer.parseInt(moveout.getStudentId()));
            moveoutVO.setStudentName(student.getName());//设置学生姓名
            Dormitory dormitory = this.dormitoryMapper.selectById(student.getDormitoryId());
            moveoutVO.setDormitoryName(dormitory.getName());
            moveouts.add(moveoutVO);
        }
        PageVO pageVO = new PageVO();
        pageVO.setData(moveouts);
        pageVO.setTotal(moveoutPage.getTotal());
        return  pageVO;
    }

    @Override
    public PageVO moveoutSearch(SearchForm searchForm) {
        Page<Moveout> moveoutPage = new Page<>(searchForm.getPage(), searchForm.getSize());
        Page<Moveout> moveoutPage1 = null;

        List<MoveoutVO> moveouts =  new ArrayList<MoveoutVO>();
        if(searchForm.getValue().equals("")){
            moveoutPage1 = moveoutMapper.selectPage(moveoutPage, null);


        }else if(searchForm.getKey().equals("studentName")){
            QueryWrapper<Moveout> moveoutQueryWrapper = new QueryWrapper<>();
            QueryWrapper<Student> queryWrapper = new QueryWrapper<>();
            queryWrapper.like("name", searchForm.getValue());
            List<Student> studentList = this.studentMapper.selectList(queryWrapper);
            List<Integer> studentIdList = new ArrayList<Integer>();
            for (Student student : studentList) {
                studentIdList.add(student.getId());
            }
            moveoutQueryWrapper.in("student_id", studentIdList);
            moveoutPage1 = moveoutMapper.selectPage(moveoutPage, moveoutQueryWrapper);
        }else if(searchForm.getKey().equals("dormitoryName")){
            QueryWrapper<Moveout> moveoutQueryWrapper = new QueryWrapper<>();
            QueryWrapper<Dormitory> dormitoryQueryWrapper = new QueryWrapper<>();
            dormitoryQueryWrapper.like("name", searchForm.getValue());
            List<Dormitory> dormitoryList = this.dormitoryMapper.selectList(dormitoryQueryWrapper);
            List<Integer> dormitoryIdList = new ArrayList<Integer>();
            for (Dormitory dormitory : dormitoryList) {
                dormitoryIdList.add(dormitory.getId());
            }
            moveoutQueryWrapper.in("dormitory_id", dormitoryIdList);
            moveoutPage1 = moveoutMapper.selectPage(moveoutPage, moveoutQueryWrapper);
        }
        for (Moveout moveout : moveoutPage1.getRecords()) {
            MoveoutVO moveoutVO = new MoveoutVO();
            BeanUtils.copyProperties(moveout, moveoutVO);//将迁出信息复制到迁出VO中
            Student student = this.studentMapper.selectById(Integer.parseInt(moveout.getStudentId()));
            moveoutVO.setStudentName(student.getName());//设置学生姓名
            Dormitory dormitory = this.dormitoryMapper.selectById(student.getDormitoryId());
            moveoutVO.setDormitoryName(dormitory.getName());
            moveouts.add(moveoutVO);
        }
        PageVO pageVO = new PageVO();
        pageVO.setData(moveouts);
        pageVO.setTotal(moveoutPage.getTotal());
        return  pageVO;

    }
}
